﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using System.Text.RegularExpressions;
using Job.Logic.Personal;
using Job.Entity;
using System.IO;

namespace Job.Web.userControls.Personal
{
    public partial class UploadPhoto : BaseCtrl
    {
        int _personinfoId = myR["resumeid"].IntValue;
        PersonalLogic pl = new PersonalLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                JobPersonInfo info = pl.GetUserPhoto(_personinfoId);
                byte[] photo = info.UserPhoto;
                string extName = info.PhotoExtName;
                if (photo != null)
                {
                    Image1.ImageUrl = WebCommon.GetApplicationPath(Request) + "Handler/GetUserPhoto.aspx?personalid=1&type=" + extName;
                }
                else
                {
                    Image1.ImageUrl = WebCommon.GetApplicationPath(Request) + "Images/resume.gif";
                }
            }
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            if (this.fileUpload.PostedFile.FileName.Equals(""))
            {
                return;
            }
            else
            {
                string _regex = @"^.+\.(jp?g|gif)$";
                Regex test = new Regex(_regex);

                if (test.IsMatch(fileUpload.PostedFile.FileName.ToLower()) == true)
                {
                    string extName = Path.GetExtension(fileUpload.PostedFile.FileName);
                    JobPersonInfo info = new JobPersonInfo();
                    info.PersonInfoId = _personinfoId;
                    byte[] photo = Functions.StreamToByteArray(fileUpload.PostedFile.InputStream);
                    info.UserPhoto = photo;
                    info.PhotoExtName = extName;
                    pl.UpDatePhoto(info);

                    Image1.ImageUrl = WebCommon.GetApplicationPath(Request) + "Handler/GetUserPhoto.aspx?personalid=" + _personinfoId + "&type=" + extName;
                }
                else
                {

                }
            }
        }
    }
}